package jdbc;

import java.sql.*;

/**
 * 执行DDL语句
 */
public class Demo07 {
    public static void main(String[] args) throws Exception {
        Connection con = null;
        Statement sta = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接对象
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "myroot");
            //定义sql语句
            String sql = "select * from account";
            //获取statement对象
            sta = con.createStatement();
            //执行sql 语句
            rs = sta.executeQuery(sql);
            //读取数据
            rs.next();
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                Double balance = rs.getDouble(3);
                System.out.println(id + "\t" + name + "\t" + balance);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7.释放资源

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (sta != null) {
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}